/*
 * 锁定时取table里的ID，更新时和锁定的ID比较如果相同更新锁定的目标table
 * and open the template in the editor.
 */
package engine.skill;

import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.lcdui.Graphics;
import mainpackage.Constant;
import mainpackage.MainLogic;

/**
 *
 * @author Administrator
 */
public class SkillBuffManager {
    //buff表

    public static Hashtable buffTable;

    public SkillBuffManager() {
        buffTable = new Hashtable();
    }

    /*
     * 添加buff
     */
    public static void addToBuffTable(int index, long objId, String id, int type, int priority, int continueTime, int passTime, byte icontype) {
        if (buffTable == null) {
            buffTable = new Hashtable();
        }
        if (buffTable.size() >= Constant.BUFF_MAX_NUMS) {
            return;
        }
//        Tools.printLog("添加buff" + objId);
        SkillBuff objBuff = (SkillBuff) buffTable.get(new Long(objId));
        //如果该id没有BUFF
        if (objBuff == null) {
            objBuff = new SkillBuff();
        }
//        int tempCount = 0;
//        for (int i = 0; i < Constant.BUFF_MAX_NUMS; i++) {
//            if (objBuff.iBuffKey[i] != null) {
//                tempCount++;
//            }
//        }
//        if (tempCount >= Constant.BUFF_MAX_NUMS) {
//            return;
//        }
        objBuff.addBuff(index, id, type, priority, continueTime, passTime, icontype);
        buffTable.put(new Long(objId), objBuff);
    }
    /*
     * 更新BUFF table 如果table中的SkillBuff里的数量为0则删除该buff
     */

    public static void updateBuffTable() {
        if (buffTable == null) {
            buffTable = new Hashtable();
        }
        if (buffTable.isEmpty()) {
            return;
        }
        SkillBuff aSkillBuff;
        Long key;
        Enumeration e2 = buffTable.keys();
        while (e2.hasMoreElements()) {
            key = (Long) (e2.nextElement());
            aSkillBuff = (SkillBuff) buffTable.get(key);
            aSkillBuff.updateBuff();
            if (!aSkillBuff.blHasBuff) {
                buffTable.remove(key);
            }
        }
    }

    public static void clearBuffTable() {
        if (buffTable == null) {
            return;
        }
        if (buffTable.isEmpty()) {
            return;
        }
        SkillBuff aSkillBuff;
        Long key;
        Enumeration e2 = buffTable.keys();
        while (e2.hasMoreElements()) {
            key = (Long) (e2.nextElement());
            if (key.longValue() == MainLogic.getInstance().gamePlayer.lPlayerID
                    || MainLogic.getInstance().uiInterFace.team.getMemberPos(key.longValue()) != -1) {
            } else {
                buffTable.remove(key);
            }
        }
    }
    //绘制buff

    public static void drawBuff(Graphics g, long key, int x, int y) {
        if (buffTable == null) {
            buffTable = new Hashtable();
        }
        if (buffTable.isEmpty()) {
            return;
        }

        SkillBuff objBuff = (SkillBuff) buffTable.get(new Long(key));
        //如果该id没有BUFF
        if (objBuff != null) {
//      objBuff = new SkillBuff();
            objBuff.drawBuff(g, x, y);
        }
    }
}
